﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.AutoMapper;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class DocumentTypeAppService : AbpZeroTemplateAppServiceBase, IDocumentTypeAppService
    {
        IRepository<DocumentType, int> documentTypeRepository;

        public DocumentTypeAppService(IRepository<DocumentType, int> _documentTypeRepository)
        {
            documentTypeRepository = _documentTypeRepository;
        }

        /// <summary>
        /// 文档类型列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<Dto.GetDocumentTypeOutPut>> GetAll()
        {
            var all = await documentTypeRepository.GetAllListAsync();
            var model = all.OrderBy(m => m.OrderNo)
                .MapTo<List<Dto.GetDocumentTypeOutPut>>();
            return model;
        }


        public async Task CreateOrEdit(Dto.CreateOrEditDocumentType input)
        {
            var doc = new DocumentType();
            if (input.Id > 0)
            {
                doc = await documentTypeRepository.GetAsync(input.Id);
            }
            else
            {
                await documentTypeRepository.InsertAsync(doc);
            }
            doc.RootName = input.RootName;
            doc.ParentName = input.ParentName;
            doc.Name = input.Name;
            doc.IsEnabled = input.IsEnabled;
            doc.Description = input.Description;
        }


        public async Task<Dto.GetDocumentTypeOutPut> Get(int documentTypeId)
        {
            var doc = await documentTypeRepository.GetAsync(documentTypeId);
            var model = doc.MapTo<Dto.GetDocumentTypeOutPut>();
            return model;
        }


        public async Task EditDocumentTypeOrder(List<KeyValuePair<int, int>> record)
        {
            foreach (var item in record)
            {
                var t =await documentTypeRepository.GetAsync(item.Key);
                t.OrderNo = item.Value;
            }
        }
    }
}
